#include "CourseManager.h"

CourseManager::CourseManager( QWidget* parent )
	: QSplitter( Qt::Vertical, parent )
{
	//Manager
	QWidget* managerWidget = new QWidget( this );
	QVBoxLayout* layout = new QVBoxLayout( managerWidget );
	addWidget( managerWidget );

	// Buttons
	QHBoxLayout* buttonLayout = new QHBoxLayout();
	layout->addItem( buttonLayout );

	QPushButton* addButton = new QPushButton( tr( "Add" ), managerWidget );
	connect( addButton, SIGNAL( pressed() ), this, SLOT( add() ) );
	buttonLayout->addWidget( addButton );

	QPushButton* removeButton = new QPushButton( tr( "Remove" ), managerWidget );
	connect( removeButton, SIGNAL( pressed() ), this, SLOT( remove() ) );
	buttonLayout->addWidget( removeButton );

	mTableView = new QTableView( this );
	connect( mTableView, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( selected( const QModelIndex& ) ) );
	mTableView->setModel( Database::instance()->courseModel() );
	addWidget( mTableView );

	mScheduler = new Scheduler( this, Database::DT_COURSE );
	addWidget( mScheduler );
}

CourseManager::~CourseManager()
{
}

void
CourseManager::add()
{	
	bool ok;
	int id = QInputDialog::getInt( this, tr( "Id" ), tr( "Course id:" ), 0, 0, 999999999, 1, &ok );
	if( ok )
	{
		QString name = QInputDialog::getText( this, tr( "Name" ), tr( "Course name:" ), QLineEdit::Normal, tr( "name" ), &ok );
		if( ok && !name.isEmpty() )
		{
			Database::instance()->query( tr( "insert into " ) + 
					                         COURSE + 
											 tr( " values(" ) + 
											 QString::number( id ) + 
											 tr( ", '" ) +
											 name +
											 tr( "', 'books', 'equipment', 'software', 'objectivies' )" ) );
		}
	}
}

void
CourseManager::remove()
{
}

void
CourseManager::selected( const QModelIndex& index )
{
	if( index.column() == 0 )
	{
		mScheduler->loadSchedule( index.data().toInt() );
	}
}